package com.bblocks.util;


import com.bblocks.enums.SysEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Deprecated
@NoArgsConstructor
@Data
@Schema(description="响应")
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    //响应状态
    @Schema(description="响应码 1=成功，其他为失败")
    protected Integer result;


    //响应消息
    @Schema(description="消息/错误消息")
	protected String message;

    //响应数据
    @Schema(description="数据")
	protected T object;

	public Result(SysEnum sysEnum) {
		this.result = sysEnum.getCode();
		this.message = sysEnum.getMessage();
	}


	public Result(Integer result, String message) {
		this.result = result;
		this.message = message;
	}

	public Result(SysEnum sysEnum, T object) {
		this.result = sysEnum.getCode();
		this.message = sysEnum.getMessage();
		this.object = object;
	}

	public Result(Integer result, String message, T object) {
		this.result = result;
		this.message = message;
		this.object = object;
	}


	/**
	 * 系统异常
	 *
	 * @return
	 */
	public static <T> Result<T> SysError() {
		return new Result(0, "系统异常");
	}

	public static <T> Result<T> SysError(SysEnum sysEnum) {
		return new Result(sysEnum.getCode(), sysEnum.getMessage());
	}

	public static <T> Result<T> SysError(String message) {
		return new Result(0, message);
	}


	public void setResult(SysEnum sysEnum, T object) {
		this.result = sysEnum.getCode();
		this.message = sysEnum.getMessage();
		this.object = object;

	}

	public void setResultEnum(SysEnum sysEnum) {
		this.result = sysEnum.getCode();
		this.message = sysEnum.getMessage();
	}

	/**
	 * 参数验证失败返回结果
	 * @param message 提示信息
	 */
	public static <T> Result validateFailed(String message) {
		return new Result(SysEnum.PARAMS_IS_NULL, message);
	}

	/**
	 * 参数验证失败返回结果
	 * @param message 提示信息
	 */
	public static <T> Result validateFailed() {
		return new Result(SysEnum.PARAMS_IS_NULL);
	}

	public static <T> Result<T> ok() {
		return restResult(null, SysEnum.SUCCESS.getCode(), SysEnum.SUCCESS.getMessage());
	}
	public static <T> Result<T> ok(String msg) {
		return restResult(null, SysEnum.SUCCESS.getCode(), msg);
	}
	public static <T> Result<T> ok(T data) {
		return restResult(data, SysEnum.SUCCESS.getCode(), SysEnum.SUCCESS.getMessage());
	}


	private static <T> Result<T> restResult(T data, Integer result, String msg) {
		Result<T> apiResult = new Result<>();
		apiResult.result = result;
		apiResult.setMessage(msg);
		apiResult.setObject(data);
		return apiResult;
	}

}
